/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dorian.atomicuml.controllers;

import com.dorian.atomicuml.dao.interfaces.UserManager;
import com.dorian.atomicuml.models.RegisterValidator;
import com.dorian.atomicuml.models.UserInfoData;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author cedric.creusot
 */
@Controller
public class RegisterUserController {
    
    @Autowired
    private UserManager userManager;
    
    @Autowired
    private MessageSource messageSource;

    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }
    
    @RequestMapping(value="/register.htm", method=RequestMethod.GET)
    public ModelAndView initForm()
    {
        ModelAndView mv = new ModelAndView("register");
        mv.addObject("userInfo", new UserInfoData());
        return mv;
    }
    
    @RequestMapping(value="/register.htm", method=RequestMethod.POST)
    public ModelAndView submitForm(@ModelAttribute("userInfo") UserInfoData userInfo,
                                   BindingResult result,
                                   SessionStatus status)
    {
        ModelAndView mv = new ModelAndView("register");;
        new RegisterValidator(this.messageSource).validate(userInfo, result);
        if (!result.hasErrors())
        {
            try{
            this.userManager.createUser(userInfo.getFirstname(),
                                        userInfo.getLastname(),
                                        userInfo.getPassword(), 
                                        userInfo.getMail());
            mv = new ModelAndView(new RedirectView("./index.htm"));
            mv.addObject("registered");
            }
            catch (Exception e)
            {
                System.out.println("Error : " + e);
                String msg = "";
                try
                {
                    msg = this.messageSource.getMessage("register.errors.mailused", null, Locale.FRENCH);
                }
                catch (Exception e2)
                {
                    System.out.println("Error 2 : " + e);
                }
                result.rejectValue("mail",
                               "errors.required",
                               new Object[] {"mail"},
                               msg);
            }
            status.setComplete();
        }
        return mv;
    }
}
